package com.svse.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.svse.annotation.OperateLog;
import com.svse.dao.UserMapper;
import com.svse.entity.UserEntity;
import com.svse.services.Closure;
import com.svse.services.UserService;
import com.svse.util.RedisUtil;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 李宇航
 * @since 2019-10-14
 */
@RestController
@RequestMapping("/userEntity")
public class UserController {
    @Autowired
    private UserService userService;


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RedisUtil redisService;


    @RequestMapping(value = "/listByPage", method = RequestMethod.POST, produces = "application/json")
    @OperateLog(description = "测试")
    public HashMap<String, Object> listByPage(int begin,int limit) {
        HashMap<String, Object> resMap = new HashMap<>();
        IPage<UserEntity> page = new Page<UserEntity>(begin, limit);
        resMap.put("data", userService.page(page));
        return resMap;
    }

    @RequestMapping(value = "/findList", method = RequestMethod.POST, produces = "application/json")
    public HashMap<String, Object> findList(int begin,int limit) {
        HashMap<String, Object> resMap = new HashMap<>();
        resMap.put("data", userMapper.findList());
        resMap.put("dat2", userMapper.selectById(1));
        return resMap;
    }


    @RequestMapping(value = "/pageList", method = RequestMethod.POST, produces = "application/json")
    public HashMap<String, Object> pageList(int begin,int limit) {
        HashMap<String, Object> resMap = new HashMap<>();
        RowBounds rowBounds=new RowBounds((begin-1)*limit,limit);
        resMap.put("data", userService.pageList(rowBounds));
        return resMap;
    }

    @RequestMapping(value = "/mpTest01", method = RequestMethod.POST, produces = "application/json")
    public IPage<UserEntity> mpTest01(){
        QueryWrapper<UserEntity> userEntityWrapper=new QueryWrapper <UserEntity>();
        userEntityWrapper.gt("age",20);
        userEntityWrapper.orderByAsc("id");
        IPage<UserEntity> userList=userService.page(new Page<>(1,3),userEntityWrapper);
        return userList;
    }
    @RequestMapping(value = "/mpTest02", method = RequestMethod.POST, produces = "application/json")
    public void mpTest02(){
        UpdateWrapper<UserEntity> userEntityWrapper=new UpdateWrapper <>();
        UserEntity user = new UserEntity();
        user.setAge(22);
        user.setName("Tom");
        userEntityWrapper.eq("id",5);
        System.out.println(userMapper.update(user,userEntityWrapper));
    }
    @RequestMapping(value = "/mpTest03", method = RequestMethod.POST, produces = "application/json")
    public IPage<UserEntity> mpTest03(){
        QueryWrapper<UserEntity> userEntityWrapper=new QueryWrapper <UserEntity>();
        userEntityWrapper.like("email","test");
        userEntityWrapper.orderByAsc("id");
        IPage<UserEntity> userList=userService.page(new Page<>(1,3),userEntityWrapper);
        return userList;
    }


    @RequestMapping("/redisAutoSet")
    @ResponseBody
    public Object redisAutoSet(String cacheKey) {
        return  redisService.getCache(cacheKey, new Closure<String, String>() {
            @Override
            public Object execute(String id) {
                // 执行你的业务逻辑
                return userService.getById(id);
            }
        });
    }
}

